package com.cardone.platform.configuration.controller;

import java.util.List;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.configuration.dto.SiteDto;
import com.cardone.platform.configuration.service.SiteService;

/**
 * 站点
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/configuration/site/")
@Controller
public class SiteController {
	/**
	 * 初始化数据绑定
	 *
	 * @param webDataBinder
	 *          数据绑定
	 */
	// @InitBinder
	// @Deprecated
	// public void initBinder(final WebDataBinder webDataBinder) {
	// }

	/**
	 * 站点：删除
	 *
	 * @param deleteSite
	 *          站点对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Callable<ReturnDataDto<String>> deleteJson(final SiteDto deleteSite) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(SiteService.class).deleteByIds(deleteSite);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, SiteController.log);
				}
			}
		};
	}

	/**
	 * 站点：分页查询
	 *
	 * @param findSite
	 *          站点对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
	public void editHtml(final SiteDto findSite, final Model model) {
		final SiteDto site = ContextHolder.getBean(SiteService.class).findById(SiteDto.class, findSite);

		model.addAttribute("site", site);
	}

	/**
	 * 站点：编辑
	 *
	 * @param findSite
	 *          站点对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<SiteDto>> findByIdJson(final SiteDto findSite, final Model model) {
		return new Callable<ReturnDataDto<SiteDto>>() {
			@Override
			public ReturnDataDto<SiteDto> call() throws Exception {
				try {
					final SiteDto site = ContextHolder.getBean(SiteService.class).findById(SiteDto.class, findSite);

					return ReturnDataUtils.newReturnData(site);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, SiteController.log);
				}
			}
		};
	}

	/**
	 * 站点：查询
	 *
	 * @param findListSite
	 *          站点对象
	 * @return 站点对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<List<SiteDto>>> findListByLikeCodeJson(final SiteDto findListSite) {
		return new Callable<ReturnDataDto<List<SiteDto>>>() {
			@Override
			public ReturnDataDto<List<SiteDto>> call() throws Exception {
				try {
					final List<SiteDto> siteList = ContextHolder.getBean(SiteService.class).findListByLikeCode(SiteDto.class, findListSite);

					return ReturnDataUtils.newReturnData(siteList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, SiteController.log);
				}
			}
		};
	}

	/**
	 * 站点：分页查询
	 *
	 * @param paginationSite
	 *          站点对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<PaginationDto<SiteDto>>> paginationByLikeCodeJson(final SiteDto paginationSite) {
		return new Callable<ReturnDataDto<PaginationDto<SiteDto>>>() {
			@Override
			public ReturnDataDto<PaginationDto<SiteDto>> call() throws Exception {
				try {
					final PaginationDto<SiteDto> sitePagination = ContextHolder.getBean(SiteService.class).paginationByLikeCode(SiteDto.class, paginationSite);

					return ReturnDataUtils.newReturnData(sitePagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, SiteController.log);
				}
			}
		};
	}

	/**
	 * 站点：分页查询
	 *
	 * @param paginationSite
	 *          站点对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
	public void paginationHtml(final SiteDto paginationSite, final Model model) {
		final PaginationDto<SiteDto> sitePagination = ContextHolder.getBean(SiteService.class).paginationByLikeCode(SiteDto.class, paginationSite);

		model.addAttribute("sitePagination", sitePagination);
	}

	/**
	 * 站点：保存
	 *
	 * @param saveSite
	 *          站点对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
	public Callable<ReturnDataDto<String>> saveJson(final SiteDto saveSite) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(SiteService.class).saveByIdOrCode(SiteDto.class, saveSite);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, SiteController.log);
				}
			}
		};
	}
}
